package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.lib.devices.sgsettingentity.setting.PanelModel;
import bb.lanxing.util.ui.GsonUtils;
import bb.lanxing.utils.PanelModelToJsonUtil;

public class DevicePanelDisplayActivity extends BaseActivity {
    private static final int REQUEST_CODE_PANEL = 2333;

    private int sportMode;
    private PanelModel PANEL_MODEL;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_panel_display);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextView title = findViewById(R.id.title);
        title.setText(getIntent().getStringExtra("title"));

        sportMode = getIntent().getIntExtra("sportMode", 0);
        PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(getIntent().getStringExtra("PANEL_MODEL"), PanelModel.class);

        CardView page1 = findViewById(R.id.page1);
        page1.setOnClickListener(v -> {
            Intent intent = new Intent(this, DevicePanelPageActivity.class);
            intent.putExtra("sportMode", sportMode);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_panel_page1));
            intent.putExtra("pageIndex", 0);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_PANEL);
        });

        CardView page2 = findViewById(R.id.page2);
        page2.setOnClickListener(v -> {
            Intent intent = new Intent(this, DevicePanelPageActivity.class);
            intent.putExtra("sportMode", sportMode);
            intent.putExtra("title", getIntent().getStringExtra("title")
                    + " " + getString(R.string.device_profiles_setting_detail_panel_page2));
            intent.putExtra("pageIndex", 1);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_PANEL);
        });
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_PANEL) {
            PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(data.getStringExtra("PANEL_MODEL"), PanelModel.class);
        }
    }
}